package com.css.dzj.dzjjcss.modules.publicity.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditService;
import com.css.dzj.dzjjcss.common.util.BeanUtil;
import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.common.util.ServiceUtil;
import com.css.dzj.dzjjcss.modules.publicity.entity.PublicityEntity;
import com.css.dzj.dzjjcss.modules.publicity.service.PublicityService;
import com.css.dzj.dzjjcss.modules.todo.entity.JcssTodoEntity;
import com.css.dzj.dzjjcss.modules.todo.service.TodoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/publicity")
public class PublicityController {

    @Autowired
    private PublicityService pService;

    @Autowired
    private HttpServletRequest request;

    //代办接口
    @Autowired
    private TodoService todoService;
    @Autowired
    private SysAuditService sysAuditService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize,@RequestParam Integer pageNo){
        String s="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<PublicityEntity> publicResults =  pService.queryPage(pageRequest);
            s = JSON.toJSONString(publicResults);
            sysAuditService.saveSuccessAudit("获取通知公告列表成功",Constant.GG, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data",publicResults);
        }catch (Exception e){
            sysAuditService.saveFailAudit("获取通知公告列表失败",Constant.GG,Constant.PT,Constant.PAGE,s,e,request);
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过id查看公式通报信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id){
        String s="";
        try{
            PublicityEntity pEntity = pService.findById(id);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("获取通知公告详情成功",Constant.GG, Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data",pEntity);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("获取通知公告详情失败",Constant.GG,Constant.PT,Constant.SELETE,s,e,request);
            return  R.error(e.getMessage());
        }
    }

    /**
     * 暂存
     */
    @PostMapping("/save")
    public R save(@RequestBody PublicityEntity pEntity){
        String s="";
        try{
            if(StringUtils.isEmpty(pEntity)){
                return R.error("公式通报参数不能为空");
            }
            pService.add(pEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("保存通知公告成功",Constant.GG, Constant.PT,Constant.ZCADD,s,request);
            return R.ok();

        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("保存通知公告失败",Constant.GG,Constant.PT,Constant.ZCADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 新建的时候直接选择发布
     */
    @PostMapping("/saverelease")
    public R saverelease(@RequestBody PublicityEntity pEntity){
        String s="";
        try {
            if(StringUtils.isEmpty(pEntity)){
                return R.error("公式通报参数不能为空");
            }
            pEntity.setReleaseTime(new Date());
            pService.saverelease(pEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("发布通知公告成功",Constant.GG, Constant.PT,Constant.FB,s,request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("发布通知公告失败",Constant.GG,Constant.PT,Constant.FB,s,e,request);
            return  R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody PublicityEntity pEntity){
        String s="";
        try {
            PublicityEntity oldpEntity = pService.findById(pEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(pEntity,oldpEntity);
            oldpEntity.setUpdateTime(new Date());
            pService.update(oldpEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("修改通知公告成功",Constant.GG, Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("修改通知公告失败",Constant.GG,Constant.PT,Constant.UPDATE,s,e,request);
            return  R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String id){
        String s="";
        try {
            if(pService.delete(id)){
                s = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("删除通知公告成功",Constant.GG, Constant.PT,Constant.DEL,s,request);
                return R.ok("操作成功!");
            }else{
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("删除通知公告失败",Constant.GG,Constant.PT,Constant.DEL,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     *多条件查询公式通报信息
     */
    @GetMapping("/gstbList")
    public R getGstbList(@RequestParam(required = false) String createUser,
                        @RequestParam(required = false) String title,
                        @RequestParam(required = false) String createUserUnit,
                        @RequestParam(required = false) String status,
                        @RequestParam(required = false) String releaseStartTime,
                        @RequestParam(required = false) String releaseEndTime,
                        @RequestParam(required = false) Integer pageSize, @RequestParam Integer pageNo) {
        String s="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<PublicityEntity> publicResults = pService.getGstbList(createUser,title,createUserUnit,status,releaseStartTime,releaseEndTime,pageRequest);
            s = JSON.toJSONString(publicResults);
            sysAuditService.saveSuccessAudit("查询通知公告成功",Constant.GG, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", publicResults);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询通知公告失败",Constant.GG,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 撤回公式通报信息
     */
    @PostMapping("/recall")
    public R recall(@RequestBody PublicityEntity pEntity){
        String s="";
        try {
            if(!"1".equals(pEntity.getStatus())){

                return R.error("未发布状态，不能撤销!");
            }
            PublicityEntity oldpEntity = pService.findById(pEntity.getId());
            oldpEntity.setStatus("2");
            oldpEntity.setUpdateTime(new Date());
            oldpEntity.setOtherTip(pEntity.getOtherTip());
            pService.update(oldpEntity);
            s = JSON.toJSONString(oldpEntity);
            sysAuditService.saveSuccessAudit("撤回通知公告成功",Constant.GG, Constant.PT,Constant.CH,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("撤回通知公告失败",Constant.GG,Constant.PT,Constant.CH,s,e,request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 发布公式通报信息
     */
    @PostMapping("/release")
    public R release(@RequestBody PublicityEntity pEntity){
        String s="";
        try {
            PublicityEntity oldpEntity = pService.findById(pEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(pEntity, oldpEntity);
            oldpEntity.setStatus("1");
            oldpEntity.setUpdateTime(new Date());
            oldpEntity.setReleaseTime(new Date());
            pService.update(oldpEntity);
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveSuccessAudit("发布通知公告成功",Constant.GG, Constant.PT,Constant.FB,s,request);
            //调取代办接口
            JcssTodoEntity jsTodoEntity = new JcssTodoEntity();
            jsTodoEntity.setStatus("1");//代表代办
            jsTodoEntity.setCreateTime(new Date());
            jsTodoEntity.setDelFlag(Constant.NO_DELETE);
            jsTodoEntity.setTitle(pEntity.getTitle());
            jsTodoEntity.setLinkId(pEntity.getId());
            //查看详情页
            jsTodoEntity.setUrl("../todolist/publick.html");
            jsTodoEntity.setLocation(ServiceUtil.getOrgName(request));
            jsTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
            todoService.add(jsTodoEntity);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(pEntity);
            sysAuditService.saveFailAudit("发布通知公告失败",Constant.GG,Constant.PT,Constant.FB,s,e,request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 向外提供接口
     * 多条件分页查询公示通报
     * @author wt
     * @date 2020\3\24 0024
     */
    @GetMapping("/termList")
    public List termList(@RequestParam String title,
                         @RequestParam String publicType,
                         @RequestParam String isPublic){
        try {
            List<PublicityEntity> list = pService.termList(title,publicType,isPublic);
            return list;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 向外提供接口
     * 分页查询公示通报
     * @author wt
     * @date 2020\3\24 0024
     */
    @GetMapping("/pageList")
    public List pageList(@RequestParam String publicType,
                         @RequestParam String isPublic) {
        try {
            List<PublicityEntity> list = pService.pageList(publicType,isPublic);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 向外提供接口
     * 查询公示通报信息详情
     * @author wt
     * @date 2020\3\24 0024
     */
    @GetMapping("/infoPublic")
    public PublicityEntity infoPublic(@RequestParam String id){
        try {
            PublicityEntity publicityEntity = pService.findById(id);
            return publicityEntity;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
